<?php

namespace app\shop\controller\welfare;


use app\shop\controller\Controller;
use app\shop\model\welfare\Welfare as WelfareModel;

use app\shop\model\welfare\Welfares as WelfaresModel;
use think\facade\Db;

/**
 * 福利记录管理
 */
class Welfare extends Controller
{
    /**
     * 列表
     */
    public function index()
    {
       $model = new WelfareModel;
       $model2 = new WelfaresModel;
        $list = $model->getList($this->postData());
        $welfare = $model2->where(array('is_delete'=>0))->select()->toArray();

        return $this->renderSuccess('', compact('list','welfare'));
    }

    public function get_month($reg_date = '')
    {
        $model = new WelfareModel;
        $list = $model->getMonth($this->postData());
        return $this->renderSuccess('', compact('list'));
    }

    public function welfare()
    {
        $model = new WelfaresModel;
        $list = $model->getList($this->postData());
        return $this->renderSuccess('', compact('list'));
    }

    public function add()
    {

        $model = new WelfaresModel;
        // 新增记录
        if ($model->add($this->postData())) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }
    public function edit($id)
    {

        if($this->request->isGet()){

            $model = WelfaresModel::detail($id);
            return $this->renderSuccess('', compact('model'));
        }
        $model = WelfaresModel::detail($id);
        // 更新记录
        if ($model->edit($this->postData())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    public function delete($id)
    {

        if (WelfaresModel::where(array('id'=>$id))->update(array('is_delete'=>1))) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError('更新失败');
    }

    public function delete_w()
    {
        $id = $this->request->param('id');
        $model = new WelfareModel;
        $model->where(array('welfare_id'=>$id))->delete();
        return $this->renderSuccess('删除成功');
    }

    public function write_off()
    {
        if($this->request->isGet()){
            $id = $this->request->param('id');
            $model = WelfareModel::where(array('welfare_id'=>$id))->find();
            return $this->renderSuccess('', compact('model'));
        }
        $model = new WelfareModel;
        $data = $this->request->param();
        unset($data['create_time']);
        $res = $model->where(array('welfare_id'=>$data['welfare_id']))->update($data);
        if($res){
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    //每日统计
    public function get_count()
    {
        $data = $this->request->param();
        $where[] = ['is_delete','=',0];
        if($data['date']){
            $times = $this->get_time($data);
            $where[] = ['create_time','between',$times['date']];
        }
        $model = new WelfareModel;
        if($data['type']==1){
            $info = $model->field("count(*) as num,FROM_UNIXTIME(create_time,'%Y-%m-%d') as gap")->where($where)->group("gap")->order("create_time desc")->select()->toArray();
        }else{
            $info = $model->field("count(*) as num,FROM_UNIXTIME(create_time,'%Y-%m') as gap")->where($where)->group("gap")->order("create_time desc")->select()->toArray();
        }

        return $this->renderSuccess('', compact('info'));
    }

    public function push()
    {
        $data = $this->request->param();
        $where[] = ['is_delete','=',0];
        if($data['date']){
            $times = $this->get_time($data);
            $where[] = ['create_time','between',$times['date']];
        }
        $model = new WelfareModel;
        if($data['type']==1){
            $info = $model->field("FROM_UNIXTIME(create_time,'%Y-%m-%d') as gap,count(*) as num")->where($where)->group("gap")->order("create_time desc")->select()->toArray();
        }else{
            $info = $model->field("FROM_UNIXTIME(create_time,'%Y-%m') as gap,count(*) as num")->where($where)->group("gap")->order("create_time desc")->select()->toArray();
        }

        $tableHeader = array('日期','订单数');
        $path = $this->excel($info,$tableHeader,'福利统计');

        return $this->renderSuccess('导出成功！',['path'=>'http://'.$_SERVER['HTTP_HOST'].$path]);
    }

    public function get_time($data)
    {
        if($data['date'][0] == $data['date'][1]){
            $time['date'][0] = strtotime($data['date'][0].' 00:00:00');
            $time['date'][1] = strtotime($data['date'][1].' 23:59:59');
        }else{
            $time['date'][0] = strtotime($data['date'][0].' 00:00:00');
            $time['date'][1] = strtotime($data['date'][1].' 23:59:59');
        }
        return $time;
    }
    /**
     * 导出Excel表格
     * @param $data 打印的数据  数组形式
     * @param $tableHeader 表头 数组形式
     * @param $fileName 保存的文件名 字符串格式
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Writer_Exception
     */
    public function excel($data,$tableHeader,$fileName)
    {
        include_once (root_path().'./extend/PHPExcel/PHPExcel.php');
        //创建对象
        $excel =  new \PHPExcel();
        //Excel表格式
        $letter = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S',
            'T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL',
            'AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');
        //填充表头信息/1是格式
        $tableHeader=$tableHeader;
        for ($i = 0; $i < count($tableHeader); $i++)
        {
            $excel->getActiveSheet()->setCellValue("$letter[$i]1", "$tableHeader[$i]");
        }

        //填充表格信息
        for ($i = 2; $i <= count($data) + 1; $i++)
        {
            $j = 0;
            foreach ($data[$i - 2] as $key => $value)
            {
                $excel->getActiveSheet()->setCellValue("$letter[$j]$i", "$value");
                $j++;
            }
        }

        //创建Excel输入对象
        $write = new \PHPExcel_Writer_Excel5($excel);
        /*header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header('Content-Disposition: attachment;filename="'.$fileName.'.xls"');
        header("Content-Transfer-Encoding:binary");*/
        $path = '/uploads/xls/'.$fileName.'.xls';
        $write->save(root_path().'/public'.$path);
        return $path;
    }


}
